package com.studysystem.controller;

import com.studysystem.dto.ProgressUpdateRequest;
import com.studysystem.entity.CourseContent;
import com.studysystem.entity.CourseProgress;
import com.studysystem.entity.User;
import com.studysystem.repository.CourseContentRepository;
import com.studysystem.repository.CourseProgressRepository;
import com.studysystem.repository.CourseRepository;
import com.studysystem.repository.UserRepository;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/progress")
public class ProgressController {
    private final CourseProgressRepository courseProgressRepository;
    private final CourseRepository courseRepository;
    private final CourseContentRepository courseContentRepository;
    private final UserRepository userRepository;

    public ProgressController(CourseProgressRepository courseProgressRepository, CourseRepository courseRepository, CourseContentRepository courseContentRepository, UserRepository userRepository) {
        this.courseProgressRepository = courseProgressRepository;
        this.courseRepository = courseRepository;
        this.courseContentRepository = courseContentRepository;
        this.userRepository = userRepository;
    }

    // 更新学习进度
    @PostMapping("/update")
    public String updateProgress(@AuthenticationPrincipal UserDetails userDetails, @RequestBody ProgressUpdateRequest request) {
        Optional<User> userOpt = userRepository.findByUsername(userDetails.getUsername());
        if (userOpt.isEmpty()) return "用户不存在";
        User user = userOpt.get();
        CourseProgress progress = courseProgressRepository.findByUserIdAndCourseId(user.getId(), request.getCourseId())
                .orElse(new CourseProgress());
        progress.setUser(user);
        progress.setCourse(courseRepository.findById(request.getCourseId()).orElse(null));
        progress.setLastContent(request.getContentId() != null ? courseContentRepository.findById(request.getContentId()).orElse(null) : null);
        progress.setProgressPercentage(request.getProgressPercentage());
        progress.setIsCompleted(request.getIsCompleted() != null ? request.getIsCompleted() : false);
        progress.setLastAccessedAt(LocalDateTime.now());
        courseProgressRepository.save(progress);
        return "进度已保存";
    }

    // 查询某门课的进度
    @GetMapping("/{courseId}")
    public CourseProgress getProgress(@AuthenticationPrincipal UserDetails userDetails, @PathVariable Long courseId) {
        Optional<User> userOpt = userRepository.findByUsername(userDetails.getUsername());
        if (userOpt.isEmpty()) return null;
        return courseProgressRepository.findByUserIdAndCourseId(userOpt.get().getId(), courseId).orElse(null);
    }

    // 查询所有进度
    @GetMapping("/all")
    public List<CourseProgress> getAllProgress(@AuthenticationPrincipal UserDetails userDetails) {
        Optional<User> userOpt = userRepository.findByUsername(userDetails.getUsername());
        if (userOpt.isEmpty()) return null;
        return courseProgressRepository.findByUserId(userOpt.get().getId());
    }
} 